#include <stdio.h>
#include "childFunction.h"
using namespace std;

void createRequest(int numChild, char* requestBuffer, std::list<Tuple>& myPrivateTuples, int* lengthOfRequest /*out*/, int* sleepTime /*out*/, int* timeout /*out*/);

void createRequest_test() {
	char requestBuffer[512];
	std::list<Tuple> myPrivateTuples; // $$GEN_LOSOWA tutaj przechowujemy co wrzucił potomek do macierzystego
	int length;

	Tuple t1;
	t1.push_back(Element("suka"));
	t1.push_back(5);
	t1.push_back(80);
	myPrivateTuples.push_back(t1);
	int sleep, select;
	createRequest(15, requestBuffer, myPrivateTuples, &length, &select, &sleep );
	printf("\n %s \n", requestBuffer);
}

void childFunction_test() {
	//void childFunction(int numChild, int* requestPipe,int* answerPipe, int startupCode,  std::ifstream* sourceRequestFile=NULL);
	int numChild=1;
	int requestPipe, answerPipe;
	std::ifstream mySourceFile;
	mySourceFile.open("/home/szymon/new/uxp1a-pipe/trunk/listOfRequests.txt");
	if(!mySourceFile.is_open()) {
		std::cout<<"nie ma takiego pliku!";
		return;
	}

	//childFunction(numChild, &requestPipe, &answerPipe, startupCode_Automatic);
	childFunction(numChild, &requestPipe, &answerPipe, startupCode_Automatic);

}

//semafor na konsole
int consoleSemaphore;
sembuf sembufWait, sembufPost;

int main() {
	//semafor na konsole
	consoleSemaphore=semget (IPC_PRIVATE, 1, 0666 | IPC_CREAT);
	sembufPost.sem_num=sembufWait.sem_num=0;
	sembufWait.sem_op=-1;
	sembufPost.sem_op=1;
	semop(consoleSemaphore, &sembufPost, 1); //Post

    srand(time(NULL));
    //createRequest_test();
    //createRequestFromFile_test();
    childFunction_test();
	return 0;
}
